package com.jm.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.reggie.common.CustomException;
import com.jm.reggie.dto.DishDto;
import com.jm.reggie.entity.Dish;
import com.jm.reggie.entity.DishFlavor;
import com.jm.reggie.mapper.DishMapper;
import com.jm.reggie.service.DishFlavorService;
import com.jm.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
    private DishFlavorService dishFlavorService;

    //新增菜品，同时保存对应的口味数据
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //设置菜品与口味的关系
        Long dishId = dishDto.getId();
        List<DishFlavor> flavorList = dishDto.getFlavors();
        flavorList.forEach(flavor->flavor.setDishId(dishId));
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavorList);
    }

    //根据id查询菜品信息和对应的口味信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto=new DishDto();
        //对象拷贝
        BeanUtils.copyProperties(dish,dishDto);
        //条件构造器
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(wrapper);
        //封装口味到dto
        dishDto.setFlavors(dishFlavorList);
        return dishDto;
    }

    //修改菜品
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //保存基本信息到dish表
        this.updateById(dishDto);

        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);

        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavorList = dishDto.getFlavors();
        flavorList.forEach(flavor->flavor.setDishId(dishDto.getId()));
        dishFlavorService.saveBatch(flavorList);
    }

    //删除菜品
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {
        //查看当前选中的商品，是否处于售卖状态，处于售卖状态则不可删除
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        int count = this.count(wrapper);
        if(count>0){
            throw new CustomException("套餐为起售状态，无法删除");
        }
        //从菜单表删除
        this.removeByIds(ids);
        //从菜单口味表删除
        LambdaQueryWrapper<DishFlavor> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper2);
    }
}
